<?php
/**
 * Created by PhpStorm.
 * User: wliu
 * Date: 14-3-30
 * Time: 下午6:33
 */

class RedisCacheUtil {
    /**
     * 删除商品
     * @param $goodsId
     */
    public static function delGoods($goodsId) {
        Yii::app()->redis->ZREM(_REDIS_SORTLIST_GOODSHOTS,$goodsId);
        Yii::app()->redis->DEL(_REDIS_HASHKEY_GOODS.$goodsId);
    }

    /**
     * 得到收藏的数
     * @param $goods
     * @return mixed
     */
    public static function getFavoriteSum($goods) {
        return Yii::app()->redis->HGET(_REDIS_HASHKEY_GOODS.$goods,_REDIS_HASHKEY_GOODS_FIELD_FAVORITESNUM);
    }

    /**
     * 得到赞的数
     * @param $goods
     * @return mixed
     */
    public static function getPriseSum($goods) {
        return Yii::app()->redis->HGET(_REDIS_HASHKEY_GOODS.$goods,_REDIS_HASHKEY_GOODS_FIELD_PRISE);
    }

    /**
     * 得到价格
     *
     * @param $goods
     * @return mixed
     */
    public static function getPrice($goods) {
        return Yii::app()->redis->HGET(_REDIS_HASHKEY_GOODS.$goods,_REDIS_HASHKEY_GOODS_FIELD_COMMENTNUM);
    }

    /**
     * 得到评论数
     *
     * @param $goods
     * @return mixed
     */
    public static function getCommentSum($goods) {
        if($result = Yii::app()->redis->HGET(_REDIS_HASHKEY_GOODS.$goods,_REDIS_HASHKEY_GOODS_FIELD_PRICE))
            return $result;
        else
            return 0;
    }

    /**
     * 得到商品的热度
     * @param $goodskey
     * @return int
     */
    public static function getGoodsHot($goodskey) {
        $gfilelds = Yii::app()->redis->HGETALL(_REDIS_HASHKEY_GOODS.$goodskey);
        $hotnum = 0;
        //当前价格
        if(!empty($gfilelds)) {
            //销售量
            if (!empty($gfilelds[_REDIS_HASHKEY_GOODS_FIELD_SALESNUM])) {
                $salesNum = $gfilelds[_REDIS_HASHKEY_GOODS_FIELD_SALESNUM];
            }
            //关注
            if (!empty($gfilelds[_REDIS_HASHKEY_GOODS_FIELD_FAVORITESNUM])) {
                $favoritesNum = $gfilelds[_REDIS_HASHKEY_GOODS_FIELD_FAVORITESNUM];
            }
            //评价
            if (!empty($gfilelds[_REDIS_HASHKEY_GOODS_FIELD_COMMENTNUM])) {
                $commentNum = $gfilelds[_REDIS_HASHKEY_GOODS_FIELD_COMMENTNUM];
            }
            //浏览数
            if (!empty($gfilelds[_REDIS_HASHKEY_GOODS_FIELD_BROWSERNUM])){
                $browserNum = $gfilelds[_REDIS_HASHKEY_GOODS_FIELD_BROWSERNUM];
            }
            //分享
            if (!empty($gfilelds[_REDIS_HASHKEY_GOODS_FIELD_SHARENUM])) {
                $shareNum = $gfilelds[_REDIS_HASHKEY_GOODS_FIELD_SHARENUM];
            }
            if (!empty($salesNum)) {
                $hotnum = $hotnum+ $salesNum*10;
            }
            if (!empty($favoritesNum)) {
                $hotnum = $hotnum+ $favoritesNum*2;
            }
            if (!empty($commentNum)) {
                $hotnum = $hotnum+ $commentNum*3;
            }
            if (!empty($browserNum)) {
                $hotnum = $hotnum+ $browserNum;
            }
            if (!empty($shareNum)) {
                $hotnum = $hotnum+ $shareNum*4;
            }
        }
        return $hotnum;
    }

    public static function cancelFavorites($gid) {
        Yii::app()->redis->HINCRBY(_REDIS_HASHKEY_GOODS.$gid,_REDIS_HASHKEY_GOODS_FIELD_FAVORITESNUM,-1);
        Yii::app()->redis->ZINCRBY(_REDIS_SORTLIST_GOODSHOTS,-2,$gid);
    }

    /**
     * 写商品
     * @param $gid
     * @param $event
     */
    public static function writeHotsCache($gid,$event) {
        $fields = "";
        $hotnum = 0;
        if ($event==_USER_ACTION_BROWSER) {
            $fields = _REDIS_HASHKEY_GOODS_FIELD_BROWSERNUM;
            $hotnum = 1;
        } else if ($event==_USER_ACTION_SALES){
            $fields = _REDIS_HASHKEY_GOODS_FIELD_SALESNUM;
            $hotnum = 10;
        } else if ($event==_USER_ACTION_COMMENT) {
            $fields = _REDIS_HASHKEY_GOODS_FIELD_COMMENTNUM;
            $hotnum = 3;
        } else if ($event==_USER_ACTION_FAVORITES) {
            $fields = _REDIS_HASHKEY_GOODS_FIELD_FAVORITESNUM;
            $hotnum = 2;
        } else if ($event==_USER_ACTION_PRISE) {
            $fields = _REDIS_HASHKEY_GOODS_FIELD_PRISE;
            $hotnum = 3;
        } else if ($event==_USER_ACTION_SHARE) {
            $fields = _REDIS_HASHKEY_GOODS_FIELD_SHARENUM;
            $hotnum = 4;
        };
        Yii::app()->redis->HINCRBY(_REDIS_HASHKEY_GOODS.$gid,$fields,1);
        Yii::app()->redis->ZINCRBY(_REDIS_SORTLIST_GOODSHOTS,$hotnum,$gid);
    }

    /**
     * 删除商品cache
     * @param $gid
     * @param $event
     */
    public static function deleteHotsCache($gid) {
        Yii::app()->redis->DEL(_REDIS_HASHKEY_GOODS.$gid);
        Yii::app()->redis->ZREM(_REDIS_SORTLIST_GOODSHOTS,$gid);
    }

    /**
     *
     */
    public static  function writePrice($gid,$price) {
        Yii::app()->redis->HSET(_REDIS_HASHKEY_GOODS.$gid,_REDIS_HASHKEY_GOODS_FIELD_PRICE,$price);
    }

    /**
     * 解锁
     * @param $gid
     */
    public static function unlock($gid) {
        Yii::app()->redis->DEl(_REDIS_HASHKEY_GOODS_LOCK.$gid);
    }

    /**
     * 解锁
     * @param $gid
     */
    public static function lock($gid) {
        Yii::app()->redis->DEl(_REDIS_HASHKEY_GOODS_LOCK.$gid);
    }
}